package excercise.recursive;

import java.util.ArrayList;
import java.util.List;

/**
 * 子集
 */
public class Question2 {

    public void solution(String str) {
        char[] arr = str.toCharArray();
        boolean[] used = new boolean[str.length()];
        List<String> r = new ArrayList<>();
        f(0, arr.length, arr, used, r);
        r.forEach(System.out::println);
    }

    public void f(int curLen, int len, char[] arr, boolean[] used, List<String> r) {
        if (curLen >= len) {
            r.add(getStr(arr, used));
            return;
        }
        used[curLen] = true;
        f(curLen + 1, len, arr, used, r);
        used[curLen] = false;
        f(curLen + 1, len, arr, used, r);
    }

    public String getStr(char[] arr, boolean[] used) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < used.length; i++) {
            if (used[i]) {
                s.append(arr[i]);
            }
        }
        return s.toString();
    }

    public static void main(String[] args) {
        Question2 question2 = new Question2();
        question2.solution("ab");
    }
}
